package com.stylefeng.guns.modular.manager.controller;

import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.constant.factory.ConstantFactory;
import com.stylefeng.guns.core.common.exception.BizExceptionEnum;
import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.node.ZTreeNode;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.manager.service.IGoodsCategoryService;
import com.stylefeng.guns.modular.system.model.Dept;
import com.stylefeng.guns.modular.system.model.GoodsCategory;
import com.stylefeng.guns.modular.system.warpper.ProductCateWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 商品分类管理控制器
 *
 * @author fengshuonan
 * @Date 2018-09-12 20:04:11
 */
@Controller
@RequestMapping("/goodsCategory")
public class GoodsCategoryController extends BaseController {

    private String PREFIX = "/manager/goodsCategory/";

    @Autowired
    private IGoodsCategoryService goodsCategoryService;

    /**
     * 跳转到商品分类管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "goodsCategory.html";
    }

    /**
     * 跳转到添加商品分类管理
     */
    @RequestMapping("/goodsCategory_add")
    public String goodsCategoryAdd() {
        return PREFIX + "goodsCategory_add.html";
    }

    /**
     * 跳转到修改商品分类管理
     */
    @RequestMapping("/goodsCategory_update/{goodsCategoryId}")
    public String goodsCategoryUpdate(@PathVariable Integer goodsCategoryId, Model model) {
        GoodsCategory goodsCategory = goodsCategoryService.selectById(goodsCategoryId);
        model.addAttribute("item",goodsCategory);
        model.addAttribute("pName",ConstantFactory.me().getProductCateName(Long.valueOf(goodsCategory.getParentId())));
        LogObjectHolder.me().set(goodsCategory);
        return PREFIX + "goodsCategory_edit.html";
    }

    /**
     * 获取商品分类管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        List<Map<String, Object>> list = this.goodsCategoryService.list(condition);
        return super.warpObject(new ProductCateWrapper(list));
    }

    /**
     * 新增商品分类管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(GoodsCategory goodsCategory) {
        String createBy = Objects.requireNonNull(ShiroKit.getUser()).getAccount();
        goodsCategory.setCreateBy(createBy);
        goodsCategory.setUpdateBy(createBy);
        goodsCategorySetPids(goodsCategory);
        goodsCategoryService.insert(goodsCategory);
        return SUCCESS_TIP;
    }

    private void goodsCategorySetPids(GoodsCategory goodsCategory) {
        if (ToolUtil.isEmpty(goodsCategory.getParentId()) || "0".equals(goodsCategory.getParentId())) {
            goodsCategory.setParentId("0");
            goodsCategory.setParentIds("[0],");
        } else {
            String pid = goodsCategory.getParentId();
            GoodsCategory temp = goodsCategoryService.selectById(Long.valueOf(pid));
            String pids = temp.getParentIds();
            goodsCategory.setParentId(pid);
            goodsCategory.setParentIds(pids + "[" + pid + "],");
        }
    }


    /**
     * 获取部门的tree列表
     */
    @RequestMapping(value = "/tree")
    @ResponseBody
    public List<ZTreeNode> tree() {
        List<ZTreeNode> tree = this.goodsCategoryService.tree();
        tree.add(ZTreeNode.createParent());
        return tree;
    }

    /**
     * 删除商品分类管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer goodsCategoryId) {
        goodsCategoryService.deleteCate(goodsCategoryId);
        return SUCCESS_TIP;
    }

    /**
     * 修改商品分类管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(GoodsCategory goodsCategory) {
        if (ToolUtil.isEmpty(goodsCategory) || goodsCategory.getId() == null) {
            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
        }
        goodsCategorySetPids(goodsCategory);
        goodsCategoryService.updateById(goodsCategory);
        return SUCCESS_TIP;
    }

    /**
     * 商品分类管理详情
     */
    @RequestMapping(value = "/detail/{goodsCategoryId}")
    @ResponseBody
    public Object detail(@PathVariable("goodsCategoryId") Integer goodsCategoryId) {
        return goodsCategoryService.selectById(goodsCategoryId);
    }
}
